//index.js
//获取应用实例
var utils = require('../../utils/util.js');
const app = getApp()

Page({
    data: {
        currentTab: 0,
        currentPage: 1,
        categoryid:""
    },
    onLoad: function() {
        var that=this;
        wx.getSystemInfo({
            success: (res) => {
                this.setData({
                    pixelRatio: res.pixelRatio,
                    windowHeight: res.windowHeight,
                    windowWidth: res.windowWidth
                })
            },
        })
        that.nationalList();
        that.categorylist();
        that.myGoodsList(that.data.currentPage, that.data.categoryid);
    },
    //分类商品切换接口
    categorylist: function (e) {
        var that = this;
        utils.http("/api/v1/category/category_list", {}, function (res) {
            that.setData({
                navData: res.datas
            })
        })
    },
    //全国售价轮播
    nationalList:function(e){
        var that = this;
        utils.http("/api/v1/storemanage/national_price", { store_id: wx.getStorageSync("userType").storeid}, function (res) {
            that.setData({
                newList: res.datas
            })
        })
    },
    myGoodsList: function (currentPage, categoryid){
        var that=this;
        utils.showLoading();
        utils.http("/api/v1/Storemanage/my_goods", {
            store_id: wx.getStorageSync("userType").storeid,
            page: currentPage,
            category_id: categoryid,
        }, function (res) {
            utils.hideLoading();
            var allpost = that.data.goodlist;
            if (allpost == undefined) {
                allpost = [];
            }
            for (var i in res.datas) {
                res.datas[i].number = 0;
                allpost.push(res.datas[i]);
            }
            if (res.datas.length < 10) {
                that.setData({
                    tishi_title: '已加载全部',
                });
            } else {
                that.setData({
                    tishi_title: '加载更多',
                });
            }
            that.setData({
                goodlist: allpost,
                ready: res.datas.length == 0 ? 1 : 2,
            })         
        })
    },
    /**
* 页面上拉触底事件的处理函数
*/
    onReachBottom: function () {
        var that = this;
        if (that.data.ready == 2) {
            var page = that.data.currentPage + 1
            that.setData({
                currentPage: page,
            })
            that.myGoodsList(page, that.data.categoryid)
        }

    },
    switchNav(event) {
        var that=this;
        var cur = event.currentTarget.dataset.current;
        var id = event.currentTarget.dataset.id;
        console.log(event)
        //每个tab选项宽度占1/5
        var singleNavWidth = this.data.windowWidth / 5;
        //tab选项居中                            
        this.setData({
            navScrollLeft: (cur - 2) * singleNavWidth
        })
        if (this.data.currentTab == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur,
                categoryid:id,
                currentPage:1,
                goodlist:[]
            })
            that.myGoodsList(that.data.currentPage, that.data.categoryid)
        }
    },

    //搜索
    onSearchTap: function(e) {
        wx.navigateTo({
            url: '../allsearch/allsearch',
        })
    },
    //全国水果进价表
    onPricetableTap:function(e){
        wx.navigateTo({
            url: '../business/pricetable/pricetable',
        })
    },
    //我的通知
    onMynoticeTap: function (e) {
        wx.navigateTo({
            url: '../farmerend/noticetype/noticetype',
        })
    },
    //订单
    onorderTap:function(e){
        wx.reLaunch({
            url: '../business/order/order',
        })
    },
    //我的
    onFarmerendpersonalTap: function (e) {
        wx.reLaunch({
            url: '../business/buspersonal/buspersonal',
        })
    },
    //返回用户商城首页
    onIndexTap: function () {
        wx.reLaunch({
            url: '../index/index',
        })
    },
})